package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.LeaveStudent;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.LeavelStuentQueryObject;
import trlygjj.tanruiliyigenjinjin.service.ILeaveStudentService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.List;

/**
 * 学员流失
 */
@Controller
@RequestMapping("leaveStudent")
public class LeaveStudentController {

    @Autowired
    private ILeaveStudentService leaveStudentService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequestMapping("")
    @PermissionName("流失学员视图")
    @RequiresPermissions("leaveStudent:index")
    public String index(){
        return "leaveStudent/list";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    @PermissionName("流失学员列表")
    @RequiresPermissions("leaveStudent:list")
    public PageResult list(LeavelStuentQueryObject qo){
        PageResult result = leaveStudentService.query(qo);
        return result;
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("listAll")
    @ResponseBody
    @PermissionName("流失学员全部")
    @RequiresPermissions("leaveStudent:listAll")
    public List<LeaveStudent> listAll(){
        return leaveStudentService.selectAll();
    }

    /**
     * 保存
     * @param leaveStudent 要保存的流失学员对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("save")
    @ResponseBody
    @PermissionName("流失学员保存")
    @RequiresPermissions("leaveStudent:save")
    public JsonResult save(LeaveStudent leaveStudent){
        JsonResult result = new JsonResult();
        try{
           leaveStudentService.insert(leaveStudent);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 审核流失学员状态
     *
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("changeState")
    @ResponseBody
    @PermissionName("流失学员审核")
    @RequiresPermissions("leaveStudent:changeState")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try{
            // 查询出需要审核的休学学员
            leaveStudentService.updateLeaveStudentState(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }
}
